/////////////////////////////////////////////////////////////////////////////////////////////////////
//High Octane
//Ian "Woody" Nicholas
//11/17/02 -
//Website: http://www.geocities.com/bigcrazyalien
//Email: ghotistix@hotmail.com
//Made with Div Games Studio 2
//http://www.div-arena.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

PROGRAM High_Octane;
GLOBAL
 STRUCT coursedata[9];
  A;
  B;
  C;
  D;
  E;
  F;
  G;
  H;
  I;
  J;
  angleA;
  angleB;
  angleC;
  angleD;
  angleE;
  angleF;
  angleG;
  angleH;
  angleI;
  angleJ;
  other;
  other2;
 END = 0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0,
       0,0,0,0,0,0,0,0;

 status=0;

 startx;
 starty;
 startangle;

 farright=0;
 farleft=10;
 farup=10;
 fardown=0;

 checkdistright;
 checkdistleft;
 checkdistup;
 checkdistdown;

 music;

 carid;

 sw=1024;
 sh=768;

 coursenum=2;
 lap;

 coursegraph;
 hardgraph;

 placex;
 placey;

 percentfps;

 sfont;
 mfont;
 lfont;

 fpsdraw;

 kph;

LOCAL
 turning;
 speed;
 speed_x;
 speed_y;
 prevx;
 prevy;
 prevspeed;
 prevangle;
 anglechange;
 speedchange;
 disp_speed;
 maxturn;
 maxspeed;

 weight;
 horsepower;
 handling;

 skid;

 ground1;
 ground2;
 ground3;
 ground4;

 wall5;
 wall6;
 wall7;
 wall8;

 carcollisionid;
 humancarcollisionid;

 framecount;

 checkdist;

 cpx1;
 cpy1;
 cpx2;
 cpy2;
 cpx3;
 cpy3;
 cpx4;
 cpy4;
 cpx5;
 cpy5;
 cpx6;
 cpy6;
 cpx7;
 cpy7;
 cpx8;
 cpy8;

 lastsectorx;
 lastsectory;
 sectorx;
 sectory;

 hittingwall;

 checkpoint1;
 checkpoint2;
 checkpoint3;
 checkpoint4;

BEGIN
 ignore_error(121);
 set_mode(1024768);
 set_fps(40,0);

 load_fpg("hioct.fpg");

// music=load_song("c:/music/dledso~1/etts.xm",1);
// song(music);

 sfont=load_fnt("vsmall.fnt");
 mfont=load_fnt("vmed.fnt");
 lfont=load_fnt("vbig.fnt");

 draw(2,54,15,0,4,4,106,11);
 fpsdraw=draw(3,15,15,0,5,5,5+percentfps,10);

 LOOP

  move_draw(fpsdraw,15,15,5,5,5+percentfps,10);

  percentfps=(fps*100)/40;

  IF (status==0)
   main();
  END

  IF (status==2)
//   options();
  END

  IF (status==4)
//   simulationmenu();
  END

  IF (status==6)
//   arcademenu();
  END

  IF (status==8)
//   trophyroom();
  END

  IF (status==10)
//   carmenu();
  END

  IF (status==12)
//   upgrademenu();
  END

  IF (status==14)
//   racemenu();
  END

  IF (status==16)
   race();
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS main();
BEGIN
 status=1;

 write(mfont,512,50,4,"High Octane");
 write(sfont,512,85,4,"Demo Version");

 write(sfont,512,384,4,"Choose your course by pressing 1-5");

 fade_on();

 LOOP

  IF (key(_1))
   coursenum=1;
   fade_off();
   delete_text(all_text);
   status=16;
  END

  IF (key(_2))
   coursenum=2;
   fade_off();
   delete_text(all_text);
   status=16;
  END

  IF (key(_3))
   coursenum=3;
   fade_off();
   delete_text(all_text);
   status=16;
  END

  IF (key(_4))
   coursenum=4;
   fade_off();
   delete_text(all_text);
   status=16;
  END

  IF (key(_5))
   coursenum=5;
   fade_off();
   delete_text(all_text);
   status=16;
  END

  IF (status<0 OR status>1)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS race();
BEGIN
 status=17;
 buildcourse();

 checkdistright=181;
 checkdistleft=181;
 checkdistdown=181;
 checkdistup=181;

 lap=1;

 fade_on();

 LOOP

  IF (key(_esc))
   fade_off();
   exit("Nee!",0);
  END

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS buildcourse();
BEGIN

 coursegraph=new_map(2560,2560,1280,1280,0);
 hardgraph=new_map(2560,2560,1280,1280,0);
 start_scroll(0,0,coursegraph,0,0,0);

 IF (coursenum==1)
  load("course1.dat",&coursedata);
 END

 IF (coursenum==2)
  load("course2.dat",&coursedata);
 END

 IF (coursenum==3)
  load("course3.dat",&coursedata);
 END

 IF (coursenum==4)
  load("course4.dat",&coursedata);
 END

 IF (coursenum==5)
  load("course5.dat",&coursedata);
 END

 carid=car();
// scroll.camera=compcar(1);
 compcar(1);
 compcar(2);
 compcar(3);
 scroll.camera=carid;

 LOOP

  IF (placex<10)
   IF (returngraph(placey)<>0)
    map_xput(0,coursegraph,returngraph(placey),(placex*256)+128,(placey*256)+128,returnangle(placey),101,0);
    map_xput(0,hardgraph,sethard(returngraph(placey)),(placex*256)+128,(placey*256)+128,returnangle(placey),101,0);
    IF (placex<farleft)
     farleft=placex;
     IF (returngraph(placey)==104)
      checkdistleft=543;
     END
    END
    IF (placex>farright)
     farright=placex;
     IF (returngraph(placey)==104)
      checkdistright=543;
     END
    END
    IF (placey<farup)
     farup=placey;
     IF (returngraph(placey)==104)
      checkdistup=543;
     END
    END
    IF (placey>fardown)
     fardown=placey;
     IF (returngraph(placey)==104)
      checkdistdown=543;
     END
    END
   END

   placex++;
  ELSE
   placex=0;
   placey++;
  END


  IF (placey>9)
   refresh_scroll(0);
   finish();
   BREAK;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS returngraph(number);
PRIVATE
 number;

BEGIN

 IF (number==0)
  return(coursedata[placex].A);
 END

 IF (number==1)
  return(coursedata[placex].B);
 END

 IF (number==2)
  return(coursedata[placex].C);
 END

 IF (number==3)
  return(coursedata[placex].D);
 END

 IF (number==4)
  return(coursedata[placex].E);
 END

 IF (number==5)
  return(coursedata[placex].F);
 END

 IF (number==6)
  return(coursedata[placex].G);
 END

 IF (number==7)
  return(coursedata[placex].H);
 END

 IF (number==8)
  return(coursedata[placex].I);
 END

 IF (number==9)
  return(coursedata[placex].J);
 END

END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS returnangle(number);
PRIVATE
 number;

BEGIN

 IF (number==0)
  return(coursedata[placex].angleA);
 END

 IF (number==1)
  return(coursedata[placex].angleB);
 END

 IF (number==2)
  return(coursedata[placex].angleC);
 END

 IF (number==3)
  return(coursedata[placex].angleD);
 END

 IF (number==4)
  return(coursedata[placex].angleE);
 END

 IF (number==5)
  return(coursedata[placex].angleF);
 END

 IF (number==6)
  return(coursedata[placex].angleG);
 END

 IF (number==7)
  return(coursedata[placex].angleH);
 END

 IF (number==8)
  return(coursedata[placex].angleI);
 END

 IF (number==9)
  return(coursedata[placex].angleJ);
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS sethard(realgraph);
PRIVATE
 realgraph;

BEGIN

 IF (realgraph==200)
  return(101);
 END

 IF (realgraph>200 AND realgraph<204)
  return(realgraph-100);
 END

 IF (realgraph>203 AND realgraph<217)
  return(103);
 END

 IF (realgraph==225)
  return(104);
 END

 IF (realgraph==226)
  return(105);
 END

END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS motionblur(region);
BEGIN
 graph=new_map(sw,sh,sw/2,sh/2,0);
 x=sw/2;
 y=sh/2;
 flags=4;

 LOOP

  screen_copy(0,0,graph,0,0,sw,sh);

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS disphard();
BEGIN
 graph=hardgraph;
 x=1280;
 y=1280;

 LOOP

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS dirt(number);
PRIVATE
 number;

BEGIN
 resolution=10;
 IF (number==3)
  x=father.cpx7;
  y=father.cpy7;
 ELSE
  x=father.cpx8;
  y=father.cpy8;
 END
 flags=4;
 angle=rand(-180000,180000);
 graph=23;

 ctype=c_scroll;

 size=75;

 LOOP

  graph++;
  x+=rand(-25,25);
  y+=rand(-25,25);

  IF (graph>28 OR out_region(id,0))
   BREAK;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS finish();
BEGIN
 graph=701;
 x=(coursedata[0].other*256)+128;
 y=(coursedata[1].other*256)+128;
 angle=coursedata[2].other;

 ctype=c_scroll;

 advance(128);

 LOOP

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS car();
BEGIN
 resolution=10;
 graph=2;
 x=((coursedata[0].other*256)+128)*10;     //+(128-81))*10;
 y=((coursedata[1].other*256)+128)*10;     //+(128-53))*10;
 angle=coursedata[2].other;
 z=-10;

 advance(-810);
 xadvance(angle+90000,530);

 ctype=c_scroll;

 write(sfont,1000,740,4,"MPH");
 write_int(mfont,950,740,4,&disp_speed);

/*
 write(sfont,502,30,5,"Horsepower Q A");
 write(sfont,502,50,5,"Weight W S");
 write(sfont,502,70,5,"Handling E D");

 write_int(sfont,522,30,3,&horsepower);
 write_int(sfont,522,50,3,&weight);
 write_int(sfont,522,70,3,&handling);
*/

 write(sfont,976,30,5,"Lap:");
 write_int(mfont,988,30,3,&lap);

 maxspeed=1500;
 maxturn=3000+(handling*100);

 horsepower=50;
 weight=50;
 handling=50;

 speedometer();
 speedotop();
 needle();

 checkpoint1=0;
 checkpoint2=0;
 checkpoint3=0;
 checkpoint4=0;

 LOOP

  prevspeed=disp_speed;
  prevangle=angle;

  disp_speed=(fget_dist(x,y,prevx,prevy)*5)/6;

  IF (kph==1)
   disp_speed=(disp_speed*1609)/1000;
  END

  prevx=x;
  prevy=y;

  get_real_point(1,&cpx1,&cpy1);
  get_real_point(2,&cpx2,&cpy2);
  get_real_point(3,&cpx3,&cpy3);
  get_real_point(4,&cpx4,&cpy4);

  get_real_point(5,&cpx5,&cpy5);
  get_real_point(6,&cpx6,&cpy6);
  get_real_point(7,&cpx7,&cpy7);
  get_real_point(8,&cpx8,&cpy8);



  IF (key(_q) AND horsepower<100)
   horsepower++;
  END
  IF (key(_a) AND horsepower>1)
   horsepower--;
  END
  IF (key(_w) AND weight<100)
   weight++;
  END
  IF (key(_s) AND weight>1)
   weight--;
  END
  IF (key(_e) AND handling<100)
   handling++;
  END
  IF (key(_d) AND handling>1)
   handling--;
  END



  IF (map_get_pixel(0,hardgraph,cpx1/10,cpy1/10)==15)
   ground1=1;
  ELSE
   ground1=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx2/10,cpy2/10)==15)
   ground2=1;
  ELSE
   ground2=0;                                                //Ground
  END

  IF (map_get_pixel(0,hardgraph,cpx3/10,cpy3/10)==15)
   ground3=1;
  ELSE
   ground3=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx4/10,cpy4/10)==15)
   ground4=1;
  ELSE
   ground4=0;
  END



  IF (map_get_pixel(0,hardgraph,cpx5/10,cpy5/10)==22)
   wall5=1;
  ELSE
   wall5=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx6/10,cpy6/10)==22)
   wall6=1;
  ELSE
   wall6=0;                                                   //Wall
  END

  IF (map_get_pixel(0,hardgraph,cpx7/10,cpy7/10)==22)
   wall7=1;
  ELSE
   wall7=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx8/10,cpy8/10)==22)
   wall8=1;
  ELSE
   wall8=0;
  END



  IF (key(_up) OR key(_down))
   IF (ground1==1)
    map_xput(0,coursegraph,61,cpx1/10,cpy1/10,angle,100,4);
   END

   IF (ground2==1)
    map_xput(0,coursegraph,61,cpx2/10,cpy2/10,angle,100,4);
   END

   IF (ground3==1)
    map_xput(0,coursegraph,61,cpx3/10,cpy3/10,angle,100,4);
    dirt(3);
   END

   IF (ground4==1)
    map_xput(0,coursegraph,61,cpx4/10,cpy4/10,angle,100,4);
    dirt(4);
   END
  END



  IF (collision(TYPE finish) AND (checkpoint1+checkpoint2+checkpoint3+checkpoint4)==4)
   lap++;
   checkpoint1=0;
   checkpoint2=0;
   checkpoint3=0;
   checkpoint4=0;
  END

  IF (skid>2)
   map_xput(0,coursegraph,61,cpx3/10,cpy3/10,angle,100,0);
   map_xput(0,coursegraph,61,cpx4/10,cpy4/10,angle,100,0);
   dirt(3);
   dirt(4);
  END

  IF (speed<-(4000+(horsepower*15)))
   speed=-(4000+(horsepower*15));
  END

  IF (NOT key(_space))
   IF (turning>maxturn)
    turning=maxturn;
   END

   IF (turning<-maxturn)
    turning=-maxturn;
   END
  ELSE
   IF (turning>maxturn*2)
    turning=maxturn*2;
   END

   IF (turning<-maxturn*2)
    turning=-maxturn*2;
   END
  END

  IF (disp_speed>maxspeed)
   speed-=500;
  END

  IF (hittingwall<1 AND carcollisionid==0)
   advance(speed/100);
  END

  IF (hittingwall<1 AND carcollisionid==0)
   IF (disp_speed>45)
    angle+=turning;
   ELSE
    angle+=((turning*disp_speed)/45);
   END
  END

  IF (key(_up))
   speed_x+=get_distx(angle,(60+(70-weight)+horsepower)/16);              //10+(weight/10));
   speed_y+=get_disty(angle,(60+(70-weight)+horsepower)/16);              //10+(weight/10));
   speed+=(75+(50-weight)+(horsepower*5)/4);
  END

  IF (hittingwall<1 AND carcollisionid==0)
   x+=speed_x/8;
   y+=speed_y/8;
  END

  IF (key(_down))
   IF (speed>1000)
    speed=((speed*16)/17);
   ELSE
    speed-=150;
   END
  END

  IF (key(_left))
   turning+=200+(handling*5);
  END

  IF (key(_right))
   turning-=200+(handling*5);
  END

  IF (key(_space))
   speed=((speed*25)/26);
   speed_x=(speed_x*92)/93;
   speed_y=(speed_y*92)/93;
  END

  IF (wall5==1)
   xadvance(fget_angle(x,y,cpx5,cpy5),-10);
   framecount++;
  END

  IF (wall6==1)
   xadvance(fget_angle(x,y,cpx6,cpy6),-10);
   framecount++;
  END

  IF (wall7==1)
   xadvance(fget_angle(x,y,cpx7,cpy7),-10);
   framecount++;
  END

  IF (wall8==1)
   xadvance(fget_angle(x,y,cpx8,cpy8),-10);
   framecount++;
  END



  IF (carcollisionid<>0)
   xadvance(fget_angle(x,y,carcollisionid.x,carcollisionid.y),
            (46-fget_dist(x,y,carcollisionid.x,carcollisionid.y))/13);
   speed=(speed*18)/19;
   framecount++;
  END



  IF (cpx5>25600)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpx5<0)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpy5>25600)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpy5<0)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpx6>25600)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpx6<0)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpy6>25600)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpy6<0)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpx7>25600)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpx7<0)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpy7>25600)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpy7<0)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpx8>25600)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpx8<0)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpy8>25600)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpy8<0)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END



  IF (hittingwall==0 AND carcollisionid==0)
   framecount=0;
  END

  IF (framecount>30)
   x+=rand(30-framecount,abs(30-framecount));
   y+=rand(30-framecount,abs(30-framecount));
  END

  speed=((speed*16)/(16+ground1+ground2+ground3+ground4));
  speed=((speed*87)/88);
  speed_x=((speed_x*52)/(53+ground1+ground2+ground3+ground4));
  speed_y=((speed_y*52)/(53+ground1+ground2+ground3+ground4));
  turning=((turning*10)/11);

  anglechange=abs(prevangle-angle)/1700;
  speedchange=abs(prevspeed-disp_speed);

  skid=speedchange+anglechange;

  hittingwall=wall5+wall6+wall7+wall8;

  carcollisionid=collision(TYPE compcar);

  IF (fget_dist(x/10,y/10,(farleft*256)+128,y/10)<checkdistleft)
   checkpoint1=1;
  END
  IF (fget_dist(x/10,y/10,(farright*256)+128,y/10)<checkdistright)
   checkpoint2=1;
  END
  IF (fget_dist(x/10,y/10,x/10,(farup*256)+128)<checkdistup)
   checkpoint3=1;
  END
  IF (fget_dist(x/10,y/10,x/10,(fardown*256)+128)<checkdistdown)
   checkpoint4=1;
  END

  IF (hittingwall<1 AND carcollisionid==0)
   FRAME;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

 END
END




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS speedometer();
BEGIN
 graph=801;
 flags=4;
 x=934;
 y=680;
 z=-20;

 LOOP

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS speedotop();
BEGIN
 graph=802;
 x=934;
 y=680;
 z=-22;

 LOOP

  IF (collision(TYPE car))
   flags=4;
  ELSE
   flags=0;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS needle();
BEGIN
 graph=803;
 x=934;
 y=680;
 z=-21;

 LOOP
                 //266 degrees
  angle=(father.disp_speed*(-775))+54000;

  IF (collision(TYPE car))
   flags=4;
  ELSE
   flags=0;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

  FRAME;
 END
END





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





PROCESS compcar(number);
PRIVATE
 number;

BEGIN
 resolution=10;
 graph=901;
 z=-10;

 x=((coursedata[0].other*256)+128)*10;
 y=((coursedata[1].other*256)+128)*10;
 angle=coursedata[2].other;

 IF (number==1)
  advance(420);
  xadvance(angle+90000,-530);
 END

 IF (number==2)
  advance(-810);
  xadvance(angle-90000,530);
 END

 IF (number==3)
  advance(420);
  xadvance(angle+90000,530);
 END

 ctype=c_scroll;

 maxturn=3000+(handling*100);

 horsepower=rand(15,85);
 weight=rand(15,85);
 handling=rand(15,85);

 LOOP

  prevspeed=disp_speed;
  prevangle=angle;

  disp_speed=fget_dist(x,y,prevx,prevy);

  IF (kph==1)
   disp_speed=(disp_speed*1609)/1000;
  END

  prevx=x;
  prevy=y;

  get_real_point(1,&cpx1,&cpy1);
  get_real_point(2,&cpx2,&cpy2);
  get_real_point(3,&cpx3,&cpy3);
  get_real_point(4,&cpx4,&cpy4);

  get_real_point(5,&cpx5,&cpy5);
  get_real_point(6,&cpx6,&cpy6);
  get_real_point(7,&cpx7,&cpy7);
  get_real_point(8,&cpx8,&cpy8);



  IF (map_get_pixel(0,hardgraph,cpx1/10,cpy1/10)==15)
   ground1=1;
  ELSE
   ground1=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx2/10,cpy2/10)==15)
   ground2=1;
  ELSE
   ground2=0;                                                //Ground
  END

  IF (map_get_pixel(0,hardgraph,cpx3/10,cpy3/10)==15)
   ground3=1;
  ELSE
   ground3=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx4/10,cpy4/10)==15)
   ground4=1;
  ELSE
   ground4=0;
  END



  IF (map_get_pixel(0,hardgraph,cpx5/10,cpy5/10)==22)
   wall5=1;
  ELSE
   wall5=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx6/10,cpy6/10)==22)
   wall6=1;
  ELSE
   wall6=0;                                                   //Wall
  END

  IF (map_get_pixel(0,hardgraph,cpx7/10,cpy7/10)==22)
   wall7=1;
  ELSE
   wall7=0;
  END

  IF (map_get_pixel(0,hardgraph,cpx8/10,cpy8/10)==22)
   wall8=1;
  ELSE
   wall8=0;
  END



  IF (speed<-(4000+(horsepower*15)))
   speed=-(4000+(horsepower*15));
  END

  IF (turning>maxturn)
   turning=maxturn;
  END

  IF (turning<-maxturn)
   turning=-maxturn;
  END

  IF (hittingwall<1)
   advance(speed/100);
  END

  IF (hittingwall<1)
   IF (disp_speed>45)
    angle+=turning;
   ELSE
    angle+=((turning*disp_speed)/45);
   END
  END

  IF (map_get_pixel(0,hardgraph,x/10,y/10)==0 OR disp_speed<20)
   speed_x+=get_distx(angle,(60+(70-weight)+horsepower)/16);              //10+(weight/10));
   speed_y+=get_disty(angle,(60+(70-weight)+horsepower)/16);              //10+(weight/10));
   speed+=(75+(50-weight)+(horsepower*5)/4);
  END

  IF (hittingwall<1)
   x+=speed_x/9;
   y+=speed_y/9;
  END

  IF (checkahead(1) > checkahead(-1))  //turn left
   turning-=200+(handling*5);
   speed=((speed*60)/(60+(disp_speed/40)));
   speed_x=(speed_x*92)/(92+(disp_speed/20));
   speed_y=(speed_y*92)/(92+(disp_speed/20));
  END

  IF (checkahead(-1) > checkahead(1)) //turn right
   turning+=200+(handling*5);
   speed=((speed*80)/(80+(disp_speed/30)));
   speed_x=(speed_x*92)/(92+(disp_speed/20));
   speed_y=(speed_y*92)/(92+(disp_speed/20));
  END



  IF (wall5==1)
   xadvance(fget_angle(x,y,cpx5,cpy5),-10);
   framecount++;
  END

  IF (wall6==1)
   xadvance(fget_angle(x,y,cpx6,cpy6),-10);
   framecount++;
  END

  IF (wall7==1)
   xadvance(fget_angle(x,y,cpx7,cpy7),-10);
   framecount++;
  END

  IF (wall8==1)
   xadvance(fget_angle(x,y,cpx8,cpy8),-10);
   framecount++;
  END



  IF (carcollisionid<>0)
   xadvance(fget_angle(x,y,carcollisionid.x,carcollisionid.y),
            (46-fget_dist(x,y,carcollisionid.x,carcollisionid.y))/13);
   speed=(speed*18)/19;
   turning-=200+(handling*5);
   framecount++;
  END

  IF (humancarcollisionid<>0)
   xadvance(fget_angle(x,y,humancarcollisionid.x,humancarcollisionid.y),
            (46-fget_dist(x,y,humancarcollisionid.x,humancarcollisionid.y))/13);
   speed=(speed*18)/19;
   turning-=200+(handling*5);
   framecount++;
  END



  IF (cpx5>25600)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpx5<0)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpy5>25600)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpy5<0)
   xadvance(fget_angle(x,y,cpx5,cpy5),-100);
   framecount++;
  END

  IF (cpx6>25600)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpx6<0)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpy6>25600)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpy6<0)
   xadvance(fget_angle(x,y,cpx6,cpy6),-100);
   framecount++;
  END

  IF (cpx7>25600)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpx7<0)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpy7>25600)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpy7<0)
   xadvance(fget_angle(x,y,cpx7,cpy7),-100);
   framecount++;
  END

  IF (cpx8>25600)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpx8<0)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpy8>25600)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END

  IF (cpy8<0)
   xadvance(fget_angle(x,y,cpx8,cpy8),-100);
   framecount++;
  END



  IF (hittingwall==0)
   framecount=0;
  END

  IF (framecount>30)
   x+=rand(30-framecount,abs(30-framecount));
   y+=rand(30-framecount,abs(30-framecount));
  END

  speed=((speed*16)/(16+ground1+ground2+ground3+ground4));
  speed=((speed*87)/88);
  speed_x=((speed_x*52)/(53+ground1+ground2+ground3+ground4));
  speed_y=((speed_y*52)/(53+ground1+ground2+ground3+ground4));
  turning=((turning*10)/11);

  anglechange=abs(prevangle-angle)/1300;
  speedchange=abs(prevspeed-disp_speed);

  skid=speedchange+anglechange;

  hittingwall=wall5+wall6+wall7+wall8;

  carcollisionid=collision(TYPE compcar);
  humancarcollisionid=collision(TYPE car);

  IF (hittingwall<1)
   FRAME;
  END

  IF (status<16 OR status>17)
   BREAK;
  END

 END
END




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




FUNCTION checkahead(dir);
PRIVATE
 dir;
 time=10;

BEGIN
 resolution=10;
 checkdist=father.disp_speed;
 x=father.x;
 y=father.y;
 graph=4;

 ctype=c_scroll;

 angle=father.angle+(dir*15000);

 LOOP

  advance(308);
  time--;

  IF (map_get_pixel(0,hardgraph,x/10,y/10)<>0
      OR x>25600
      OR x<0
      OR y>25600
      OR y<0)
   return(time);
  END

  IF (time<1)
   return(0);
  END

  IF (status<16 OR status>17)
   BREAK;
  END

 END
END
